package cn.stylefeng.guns.modular.model;


import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @author Exrickx
 * 前后端交互数据标准
 */
@Data
public class Result<T> implements Serializable {

    public static final String DEFAULT_SUCCESS_MESSAGE = "请求成功";

    public static final String DEFAULT_ERROR_MESSAGE = "网络异常";

    public static final Integer DEFAULT_SUCCESS_CODE = 200;

    public static final Integer DEFAULT_ERROR_CODE = 500;


    private static final long serialVersionUID = 1L;

    /**
     * 请求是否成功
     */
    private Boolean success;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 时间
     */
    private Date currentTime = new Date();

    /**
     * 响应对象
     */
    private T data;


    public Result() {
    }

    public Result(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }


    //<editor-fold desc="请求成功返回">
    public static <T> Result<T> success() {
        return new Result<T>(true, DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, null);
    }

    public static <T> Result<T> success(T object) {
        return new Result<T>(true, DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, object);
    }

    public static <T> Result<T> success(Integer code, String message, T object) {
        return new Result<T>(true, code, message, object);
    }
    //</editor-fold>


    //<editor-fold desc="请求失败返回">
    public static <T> Result<T> error(String message) {
        return new Result<T>(false, DEFAULT_ERROR_CODE, message, null);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<T>(false, code, message, null);
    }

    public static <T> Result<T> error(Integer code, String message, T object) {
        return new Result<T>(false, code, message, object);
    }
    //</editor-fold>

}
